using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.Web.BL.Rss;
using NTTHAO.CommonWeb;

public partial class admin_webparts_Plug_ins_RssNews_RssSiteInput : System.Web.UI.UserControl
{
    public RssSite _Site = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnSave.Text = this.btnSave2.Text = LanguageUtility.GetLanguageText("SAVE", Session);
        this.chkActive.Text = LanguageUtility.GetLanguageText("ACTIVE", Session);
        try
        {
            int siteID = WebGlobal.GetSite().ID;
            int langID = LanguageUtility.GetLanguageID();

            if ("editsite".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                _Site = new RssSite(Convert.ToInt32(Request.QueryString["site"].Trim()));
                if (_Site.ID < 1)
                {
                    throw new Exception("This function is not supported.");
                }

                if (!this.IsPostBack)
                {
                    txtName.Text = _Site.SiteName;
                    txtURL.Text = _Site.SiteDomain;
                    cmbCulture.Text = _Site.Culture;
                    cmbTimeZone.Text = _Site.TimeZone.ToString();
                    chkDescEncoded.Checked = _Site.DescriptionEncoded;
                    txtShortcus.Text = _Site.ShortcutIcon;
                    chkActive.Checked = _Site.Activated;
                }
            }
        }
        catch //(Exception ex)
        {
            Response.Redirect("?type=invalidurl&ReturnUrl=" + Server.UrlEncode(Request.RawUrl), false);
            return;
        }
    }

    public void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            RssSite site = null;
            if ("addsite".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                site = new RssSite();
                site.DefaultLanguage = LanguageUtility.GetLanguageID();
                site.SiteID = WebGlobal.GetSite().ID;
            }
            else if ("editsite".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                site = new RssSite(Convert.ToInt32(Request.QueryString["site"].Trim()));
            }
            else
            {
                throw new Exception("This function is not supported.");
            }

            site.SiteName = txtName.Text.Trim();
            site.SiteDomain = txtURL.Text.Trim();
            site.Culture = cmbCulture.SelectedValue;
            int timeZone = site.TimeZone;
            Int32.TryParse(cmbTimeZone.SelectedValue, out timeZone);
            site.TimeZone = timeZone;
            site.DescriptionEncoded = chkDescEncoded.Checked;
            site.ShortcutIcon = txtShortcus.Text.Trim();
            site.Activated = chkActive.Checked;

            if ("addsite".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                site.Insert();
            }
            else if ("editsite".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                site.Update();
            }

            string bUrl = "?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&item=" + Request.QueryString["item"] + "&site=" + site.ID.ToString();
            Response.Redirect(bUrl, false);
            return;
        }
        catch (Exception ex)
        {
            HtmlGenericControl ctrl = new HtmlGenericControl("div");
            ctrl.InnerHtml = "<p>" + ex.Message + "</p><p>" + ex.StackTrace + "</p>";
            this.Controls.Add(ctrl);
        }
    }
}
